package vector

import (
	"context"
	"time"

	"github.com/qdrant/go-client/qdrant"
	"go-file-perception-model/internal/model"
)

// VectorDB 向量数据库接口
type VectorDB interface {
	// 集合操作
	CreateCollection(ctx context.Context, collectionName string, vectorSize int) error
	CollectionExists(ctx context.Context, collectionName string) (bool, error)
	SetCollection(collectionName string)
	GetCollection() string
	GetCollectionInfo(ctx context.Context, collectionName string) (*qdrant.CollectionInfo, error)
	
	// 点操作
	InsertPoints(ctx context.Context, points []*model.FileChunk) error
	SearchPoints(ctx context.Context, vector []float32, limit int, threshold float32, filter *Filter) ([]*model.SearchResult, error)
	DeletePoints(ctx context.Context, ids []string) error
	DeletePointsByFilter(ctx context.Context, filter *Filter) error
	DeleteAllPoints(ctx context.Context) error
	CountPoints(ctx context.Context, filter *Filter) (uint64, error)
	GetPointByID(ctx context.Context, id string) (*model.SearchResult, error)
	
	// 关闭连接
	Close() error
}

// Filter 过滤器
type Filter struct {
	Must []Condition `json:"must"`
	Should []Condition `json:"should"`
	MustNot []Condition `json:"must_not"`
}

// Condition 条件
type Condition struct {
	Key string `json:"key"`
	Match *MatchValue `json:"match"`
}

// MatchValue 匹配值
type MatchValue struct {
	Value interface{} `json:"value"`
}

// SearchResult 搜索结果
type SearchResult struct {
	ChunkID   string  `json:"chunk_id"`
	Content   string  `json:"content"`
	Score     float32 `json:"score"`
	ChunkNo   int     `json:"chunk_no"`
	StartPos  int     `json:"start_pos"`
	EndPos    int     `json:"end_pos"`
	StartLine int     `json:"start_line"`
	EndLine   int     `json:"end_line"`
	FileID    string  `json:"file_id"`
}

// FileChunk 文件分块
type FileChunk struct {
	ID        string    `json:"id"`
	FileID    string    `json:"file_id"`
	Content   string    `json:"content"`
	Vector    []float32 `json:"vector"`
	ChunkNo   int       `json:"chunk_no"`
	StartPos  int       `json:"start_pos"`
	EndPos    int       `json:"end_pos"`
	StartLine int       `json:"start_line"`
	EndLine   int       `json:"end_line"`
	CreatedAt time.Time `json:"created_at"`
	UpdatedAt time.Time `json:"updated_at"`
}

// SystemStats 系统统计信息
type SystemStats struct {
	TotalFiles      int       `json:"total_files"`
	IndexedFiles    int       `json:"indexed_files"`
	TotalChunks     int       `json:"total_chunks"`
	IndexedChunks   int       `json:"indexed_chunks"`
	TotalVectors    int       `json:"total_vectors"`
	VectorDimension int       `json:"vector_dimension"`
	LastUpdateTime  time.Time `json:"last_update_time"`
	Uptime          time.Time `json:"uptime"`
}